// pages/addAddress/addAddress.js
var tcity = require("../../utils/citys.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bean: "",
    multiIndex: [0, 0, 0],
    proList: [],
    cityList: [],
    townList: [],
    ios: false,
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false
  },
  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad: function (options) {
    var that = this;
    if (options.bean) {
      that.setData({
        bean: JSON.parse(options.bean)
      })
    } else {
      that.data.bean = new Object()
      that.data.bean.ID = 0
      that.data.bean.uID = wx.getStorageSync("userId")
      that.data.bean.isdefault = false
      that.data.bean.name = ""
      that.data.bean.address = ""
      that.data.bean.phone = ""
    }
    tcity.init(that);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    // console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    var province = ''; var city = ''; var county = '';
    if (that.data.bean.id) {
      province = that.data.bean.sheng,
        city = that.data.bean.city,
        county = that.data.bean.quyu
    } else {
      province = cityData[0].name,
        city = cityData[0].sub[0].name,
        county = cityData[0].sub[0].sub[0].name
    }
    that.setData({
      bean: that.data.bean,
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': province,
      'city': city,
      'county': county,
      'code': cityData[0].sub[0].sub[0].code
    })
    // console.log('初始化完成');


  },

  contactInput: function (e) {
    this.data.bean.name = e.detail.value
  },
  mobileInput: function (e) {
    this.data.bean.phone = e.detail.value
  },
  dressInput: function (e) {
    this.data.bean.address = e.detail.value
  },
  defultTap: function () {

    this.data.bean.isdefault = !this.data.bean.isdefault
    this.setData({
      bean: this.data.bean
    })
  },
  /**
   * 保存
   */
  baoCun: function () {
    var that = this
    var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!that.data.bean.name) {
      wx.showToast({
        title: '请输入联系人',
        icon: "none"
      })
      return
    }
    if (!reg.exec(this.data.bean.phone)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号码',
        showCancel: false
      })
      return
    }
    if (!that.data.bean.address) {
      wx.showToast({
        title: '请输入详细地址',
        icon: "none"
      })
      return
    }
    if (that.data.bean.id) {
      var aid = that.data.bean.id
    } else {
      var aid = ''
    }
    wx.showLoading({
      title: '加载中……',
      mask: true
    })
    // return
    wx.request({
      url: app.d.path + "My/add_address",
      data: {
        'aid': aid,
        'openid': app.d.openid,
        "name": that.data.bean.name,
        "phone": that.data.bean.phone,
        "isdefault": that.data.bean.isdefault,
        "sheng": that.data.province,
        "city": that.data.city,
        "quyu": that.data.county,
        "adds": that.data.bean.address,
        "xq": that.data.value
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        console.log("修改默认", res.data)
        //请求成功
        if (res.data.status == 1) {
          wx.hideLoading()
          wx.navigateBack({
            delta: 1
          })
        } else { //请求失败      
          wx.hideLoading()
          setTimeout(function () {
            wx.showToast({
              title: res.data.err,
            })
          }, 300)
        }
      },
      fail: function (res) {
        wx.hideLoading()
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    })
  },



})